home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / read-expenses.c < prev    next >
C/C++ Source or Header  |  1997-08-08  |  5KB  |  154 lines

  1. /* read-expenses.c: Sample code to translate Pilot Expense database into generic format
  2.  *
  3.  * Copyright (c) 1997, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "pi-source.h"
  13. #include "pi-socket.h"
  14. #include "pi-expense.h"
  15. #include "pi-dlp.h"
  16.  
  17.                   
  18. int main(int argc, char *argv[])
  19. {
  20.   struct pi_sockaddr addr;
  21.   int db;
  22.   int sd;
  23.   int i;
  24.   struct PilotUser U;
  25.   int ret;
  26.   unsigned char buffer[0xffff];
  27.   unsigned char buffer2[0xffff];
  28.   struct ExpenseAppInfo tai;
  29.   struct ExpensePref tp;
  30.   
  31.   if (argc < 2) {
  32.     fprintf(stderr,"usage:%s %s\n",argv[0],TTYPrompt);
  33.     exit(2);
  34.   }
  35.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  36.     perror("pi_socket");
  37.     exit(1);
  38.   }
  39.     
  40.   addr.pi_family = PI_AF_SLP;
  41.   strcpy(addr.pi_device,argv[1]);
  42.   
  43.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  44.   if(ret == -1) {
  45.     perror("pi_bind");
  46.     exit(1);
  47.   }
  48.  
  49.   ret = pi_listen(sd,1);
  50.   if(ret == -1) {
  51.     perror("pi_listen");
  52.     exit(1);
  53.   }
  54.  
  55.   sd = pi_accept(sd, 0, 0);
  56.   if(sd == -1) {
  57.     perror("pi_accept");
  58.     exit(1);
  59.   }
  60.  
  61.   /* Ask the pilot who it is. */
  62.   dlp_ReadUserInfo(sd,&U);
  63.   
  64.   /* Tell user (via Pilot) that we are starting things up */
  65.   dlp_OpenConduit(sd);
  66.  
  67.   /* Note that under PalmOS 1.x, you can only read preferences before the DB is opened */
  68.   ret = dlp_ReadAppPreference(sd, Expense_Creator, Expense_Pref, 1, 0xffff, buffer, 0, 0);
  69.   
  70.   /* Open the ToDo database, store access handle in db */
  71.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "ExpenseDB", &db) < 0) {
  72.     puts("Unable to open ExpenseDB");
  73.     dlp_AddSyncLogEntry(sd, "Unable to open ExpenseDB.\n");
  74.     exit(1);
  75.   }
  76.   
  77.   if (ret >= 0) {
  78.     unpack_ExpensePref(&tp, buffer, 0xffff);
  79.     i = pack_ExpensePref(&tp, buffer2, 0xffff);
  80.     fprintf(stderr, "Orig prefs, %d bytes:\n", ret);
  81.     dumpdata(buffer, ret);
  82.     fprintf(stderr, "New prefs, %d bytes:\n", ret);
  83.     dumpdata(buffer2, i);
  84.     fprintf(stderr, "Expense prefs, current category %d, default category %d\n",
  85.         tp.currentCategory, tp.defaultCategory);
  86.     fprintf(stderr, "  Note font %d, Show all categories %d, Show currency %d, Save backup %d\n",
  87.         tp.noteFont, tp.showAllCategories, tp.showCurrency, tp.saveBackup);
  88.     fprintf(stderr, "  Allow quickfill %d, Distance unit %d, Currencies:\n",
  89.         tp.allowQuickFill, tp.unitOfDistance);
  90.     for(i=0;i<7;i++) {
  91.       fprintf(stderr, " %d", tp.currencies[i]);
  92.     }
  93.     fprintf(stderr, "\n");
  94.   }
  95.   
  96.  
  97.   ret = dlp_ReadAppBlock(sd, db, 0, buffer, 0xffff);
  98.   unpack_ExpenseAppInfo(&tai, buffer, 0xffff);
  99.   i = pack_ExpenseAppInfo(&tai, buffer2, 0xffff);
  100.   fprintf (stderr, "Orig length %d, new length %d, orig data:\n", ret, i);
  101.   dumpdata(buffer,ret);
  102.   fprintf (stderr, "New data:\n");
  103.   dumpdata(buffer2, i);
  104.   
  105.   fprintf(stderr, "Expense app info, sort order %d\n", tai.sortOrder);
  106.   fprintf(stderr, " Currency 1, name '%s', symbol '%s', rate '%s'\n",
  107.       tai.currencies[0].name, tai.currencies[0].symbol, tai.currencies[0].rate);
  108.   fprintf(stderr, " Currency 2, name '%s', symbol '%s', rate '%s'\n",
  109.       tai.currencies[1].name, tai.currencies[1].symbol, tai.currencies[1].rate);
  110.   fprintf(stderr, " Currency 3, name '%s', symbol '%s', rate '%s'\n",
  111.       tai.currencies[2].name, tai.currencies[2].symbol, tai.currencies[2].rate);
  112.   fprintf(stderr, " Currency 4, name '%s', symbol '%s', rate '%s'\n",
  113.       tai.currencies[3].name, tai.currencies[3].symbol, tai.currencies[3].rate);
  114.   
  115.   for (i=0;1;i++) {
  116.       struct Expense t;
  117.       int attr, category;
  118.                                  
  119.       int len = dlp_ReadRecordByIndex(sd, db, i, buffer, 0, 0, &attr, &category);
  120.       if(len<0)
  121.           break;
  122.           
  123.       /* Skip deleted records */
  124.       if((attr & dlpRecAttrDeleted) || (attr & dlpRecAttrArchived))
  125.           continue;
  126.           
  127.     unpack_Expense(&t, buffer, len);
  128.     ret = pack_Expense(&t, buffer2, 0xffff);
  129.     fprintf(stderr, "Orig length %d, data:\n", len);
  130.     dumpdata(buffer, len);
  131.     fprintf(stderr, "New length %d, data:\n", ret);
  132.     dumpdata(buffer2, ret);
  133.     
  134.     fprintf(stderr,"Category: %s\n", tai.category.name[category]);
  135.     fprintf(stderr,"Type: %d, Payment: %d, Currency: %d\n", t.type, t.payment, t.currency);
  136.     fprintf(stderr,"Amount: '%s', Vendor: '%s', City: '%s'\n",
  137.         t.amount ?t.amount: "<None>", t.vendor ? t.vendor: "<None>", t.city ?t.city: "<None>");
  138.     fprintf(stderr,"Attendees: '%s', Note: '%s'\n", t.attendees ?t.attendees: "<None>", t.note ?t.note: "<None>");
  139.     fprintf(stderr,"Date: %s", asctime(&t.date));
  140.     fprintf(stderr,"\n");
  141.  
  142.     free_Expense(&t);
  143.   }
  144.  
  145.   /* Close the database */
  146.   dlp_CloseDB(sd, db);
  147.  
  148.   dlp_AddSyncLogEntry(sd, "Read expenses from Pilot.\n");
  149.  
  150.   pi_close(sd);  
  151.   exit(0);
  152. }
  153.  
  154.